package com.zjdiepu.www.proxy.biz.impl.transfer;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.proxy.model.ext.transfer.TransferYwBusinessCarExt;
import com.zjdiepu.www.proxy.dao.transfer.TransferYwBusinessCarMapper;
import com.zjdiepu.www.proxy.model.dto.transfer.TransferYwBusinessCar;
import com.zjdiepu.www.proxy.biz.transfer.ITransferYwBusinessCarService;

/**
 * 中转_yw_车辆信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 17时50分<br/>
 * @since 1.0<br/>
 */
@Service
public class TransferYwBusinessCarServiceImpl extends BaseService implements ITransferYwBusinessCarService {
	public static final Logger logger = LoggerFactory.getLogger(TransferYwBusinessCarServiceImpl.class);

	@Autowired
	private TransferYwBusinessCarMapper transferYwBusinessCarMapper;
	
	@Override
	public void saveUpdateAndDelete(TransferYwBusinessCar transferYwBusinessCar, OperateType operType) {
		switch (operType) {
		case add:
			transferYwBusinessCar.initInsertData(transferYwBusinessCar.getModifier(), transferYwBusinessCar.getModifierName());
			transferYwBusinessCarMapper.insert(transferYwBusinessCar);
			break;
		case edit:
			transferYwBusinessCar.initUpdateData(transferYwBusinessCar.getModifier(), transferYwBusinessCar.getModifierName());
			// 影响的记录数
			int updateCount = transferYwBusinessCarMapper.updateByPrimaryKeySelective(transferYwBusinessCar);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			transferYwBusinessCar.initDeleteData(transferYwBusinessCar.getModifier(), transferYwBusinessCar.getModifierName());
			// 影响的记录数
			int deleteCount = transferYwBusinessCarMapper.updateByPrimaryKeySelective(transferYwBusinessCar);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<TransferYwBusinessCar> selectByExt(TransferYwBusinessCarExt transferYwBusinessCarExt) {
		return transferYwBusinessCarMapper.selectByExt(transferYwBusinessCarExt);
	}
	
	@Override
	public int countByExt(TransferYwBusinessCarExt transferYwBusinessCarExt){
		return transferYwBusinessCarMapper.countByExt(transferYwBusinessCarExt);
	}
	
	@Override
	public TransferYwBusinessCar selectById(String id) {
		return transferYwBusinessCarMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			transferYwBusinessCarMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(TransferYwBusinessCar transferYwBusinessCar) {
		return transferYwBusinessCarMapper.countRefined(transferYwBusinessCar);
	}

	@Override
	public TransferYwBusinessCar selectRefined(TransferYwBusinessCar transferYwBusinessCar) {
		return transferYwBusinessCarMapper.selectRefined(transferYwBusinessCar);
	}
	
	@Override
	public int deletedRefined(TransferYwBusinessCar transferYwBusinessCar) {
		return transferYwBusinessCarMapper.deletedRefined(transferYwBusinessCar);
	}

	@Override
	public boolean repeat(TransferYwBusinessCar transferYwBusinessCar) {
		int count = countRefined(transferYwBusinessCar);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(transferYwBusinessCar.getId())) {
			return true;
		}
		TransferYwBusinessCar dbItem = selectRefined(transferYwBusinessCar);
		if(null == dbItem) {
			return false;
		}
		return !transferYwBusinessCar.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return transferYwBusinessCarMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<TransferYwBusinessCar> transferYwBusinessCars) {
		return transferYwBusinessCarMapper.insertBatch(transferYwBusinessCars);
	}
}
